package com.adams.app.ui.fragment;

import java.io.File;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.adams.app.AppData;
import com.adams.app.R;
import com.adams.app.common.Global;
import com.adams.app.listener.MyLocationListener;
import com.adams.app.ui.NearbyActivity;
import com.adams.app.utils.ShakeListener;
import com.adams.app.utils.ShakeListener.OnShakeListener;
import com.adams.app.utils.SoundManager;
import com.baidu.location.LocationClient;

/**
 * @description 实现摇一摇查找
 * 
 * @author ChengBin
 * 
 */
public class ShakeFragment extends Fragment {

	private static final String ARG_SECTION_NUMBER = "section_number";

	public ShakeFragment() {
	}

	public static ShakeFragment newInstance(int sectionNumber) {
		ShakeFragment fragment = new ShakeFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		fragment.setArguments(args);
		return fragment;
	}

	private TextView tvLocationText;
	private ImageView shakeImageView;
	private LocationClient mLocationClient;
	private ShakeListener mShakeListener;
	private SoundManager mSoundManager;
	private Animation animation;
	private TextView cameraText;
	private View layoutTop;
	private View layoutBottom;
	private TextView recommendText;
	private TextView nearbyText;
	private ProgressBar loadingProgress;
	private AppData baseApplication;
	private boolean isFirst = true;
	private Handler handler = new Handler();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_foods, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		// 初始化布局
		initView(view);
		initLocation();
		initShake();
	}

	private void initShake() {
		isFirst = true;
		mSoundManager = new SoundManager();
		mSoundManager.initSounds(getActivity());
		mSoundManager.addSound(1, R.raw.shake);
		mShakeListener = new ShakeListener(getActivity());
		mShakeListener.setOnShakeListener(new Shake());
	}

	/**
	 * 定位当前位置
	 */
	public void initLocation() {
		baseApplication = (AppData) getActivity().getApplication();
		mLocationClient = baseApplication.getLocationClient();
		mLocationClient.registerLocationListener(new MyLocationListener(
				handler, tvLocationText, loadingProgress));
		if (!mLocationClient.isStarted()) {
			mLocationClient.start();
		}
		handler.postDelayed(new Runnable() {
			public void run() {
				baseApplication.requestLocation();
			}
		}, 500);
	}

	/**
	 * @description 初始化布局
	 * @author chengbin
	 * @param view
	 */
	public void initView(View view) {
		shakeImageView = (ImageView) view.findViewById(R.id.butnFist);
		layoutTop = ((ViewStub) view.findViewById(R.id.layoutLocation))
				.inflate();
		loadingProgress = (ProgressBar) layoutTop
				.findViewById(R.id.loading_icon);
		tvLocationText = (TextView) layoutTop.findViewById(R.id.tvLocation);
		tvLocationText.setOnClickListener(locationClickListener);
		layoutBottom = ((ViewStub) view.findViewById(R.id.layoutBottom))
				.inflate();
		recommendText = (TextView) layoutBottom
				.findViewById(R.id.butnRecommend);
		nearbyText = (TextView) layoutBottom.findViewById(R.id.butnNearby);
		nearbyText.setOnClickListener(nearTextClick);
		recommendText.setOnClickListener(recommendClickListener);
		cameraText = (TextView) layoutBottom.findViewById(R.id.butnCamera);
		cameraText.setOnClickListener(cameraCliclListener);
		// tvShakerText = (TextView) view.findViewById(R.id.tvShakerCount);
		animation = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);

	}

	// 附近搜索
	OnClickListener nearTextClick = new OnClickListener() {
		public void onClick(View v) {
			if (Global.isLocation) {
				Intent intent = new Intent(getActivity(), NearbyActivity.class);
				intent.putExtra(Global.FLAG, Global.SEARCH_FLAG);
				startActivity(intent);
			} else {
				Toast.makeText(baseApplication, "请稍等，正在定位...",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	OnClickListener locationClickListener = new OnClickListener() {
		public void onClick(View view) {
			if (Global.isLocation) {
				/*String address = ((TextView)view).getText().toString();
				Intent intent = new Intent(getActivity(), MyMapActivity.class);
				intent.putExtra(Global.ADDRESS, address);
				intent.putExtra(Global.NAME, address);
				startActivity(intent);*/
			} else {
				Toast.makeText(baseApplication, "请稍等，正在定位...",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	// 拍照监听
	OnClickListener cameraCliclListener = new OnClickListener() {
		public void onClick(View view) {
			try {
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				File out = new File(Environment.getExternalStorageDirectory(),
						"camera.jpg");
				Uri uri = Uri.fromFile(out);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
				startActivityForResult(intent, 0);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	// 搜索监听
	OnClickListener recommendClickListener = new OnClickListener() {
		public void onClick(View v) {
			// Intent intent = new Intent(getActivity(),
			// RestaurantFragment.class);
			// startActivity(intent);
		}
	};

	// 以下重写Fragment生命周期
	@Override
	public void onResume() {
		super.onResume();
		if (mShakeListener != null) {
			mShakeListener.start();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		isFirst = true;
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.stop();
		}
		if (mShakeListener != null) {
			mShakeListener.stop();
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.stop();
		}
		if (mShakeListener != null) {
			mShakeListener.stop();
		}
	}

	@Override
	public void onDestroy() {
		super.onStop();
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.stop();
		}
		if (mShakeListener != null) {
			mShakeListener.stop();
		}
		Global.isLocation = false;
	}

	/**
	 * @description 摇一摇监听接口实现
	 * 
	 * @author ChengBin
	 * 
	 */
	class Shake implements OnShakeListener {
		@Override
		public void onShake() {
			synchronized (ShakeFragment.class) {
				// 如果定位成功
				if (Global.isLocation && isFirst) {

					isFirst = false;
					shakeImageView.startAnimation(animation);
					mSoundManager.playSound(1);
					mShakeListener.getVibrator().vibrate(500);
					handler.postDelayed(new Runnable() {
						@Override
						public void run() {
							Intent intent = new Intent(getActivity(),
									NearbyActivity.class);
							intent.putExtra(Global.FLAG, Global.SHAKE_FLAG);
							startActivity(intent);
						}
					}, 1200);

				}
			}
		}
	}

}